package com.glb;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.Formatter;
import java.util.TimeZone;

public class DateTest {

  public static void main(String args[])
  {
      // initialize Date class
      Date date = new Date();

      // initialize Formatter class
      Formatter fm = new Formatter();

      //"format" use to format the month name in to string
      // from the date object
      //"%tb" is use to print the Month in MMM form
      // fm.format("%tb", date);
      fm.format("%tB", date);

      System.out.println(fm);
  }

  public static void main7(String[] args) {
    Date date = new Date();

    LocalDateTime d = LocalDateTime.now();

    ZonedDateTime UTCtime = d.atZone(ZoneId.of("Asia/Shanghai"));
    // "d" is the current date and
    // "ZonedDateTime" accepts "d" as UTC
    // "atZone" specifies the time zone
    System.out.println(UTCtime);  // 2024-07-12T11:19:24.573997Z[UTC]

    // converting to IST
    ZonedDateTime ISTtime = UTCtime.withZoneSameInstant(
        ZoneId.of("Asia/Kolkata"));
    // "withZoneSameInstant" convert the time
    // to given time zone

    System.out.println(ISTtime);  // 2024-07-12T16:49:24.573997+05:30[Asia/Kolkata]
    // print the time and date
  }

  public static void main6(String args[]) {
    SimpleDateFormat formatDate = new SimpleDateFormat(
        "dd/MM/yyyy  HH:mm:ss z");
    // "SimpleDateFormat" class initialize with object
    // "formatDate" this class acceptes the format of
    // date and time as ""dd/MM/yyyy" and "HH:mm:ss z""
    // "z" use for print the time zone

    Date date = new Date();
    // initialize "Date" class

    formatDate.setTimeZone(TimeZone.getTimeZone("IST"));
    // converting to IST or format the Date as IST

    System.out.println(formatDate.format(date)); // 12/07/2024 08:18:30 IST
    // print formatted date and time
  }

  public static void main5(String args[]) {
    Date current_Date = new Date();
    // "Date" class
    // "current_Date" is Date object

    System.out.println(current_Date); // Fri Jul 12 10:43:05 CST 2024
    // print the time and date
  }

  // Main driver method
  public static void main4(String[] args) {
    // Create an object of LoacalDate type
    LocalDate date = LocalDate.now();
    // .now() method to tore the current date
    // Print current date
    System.out.println(date); // 2024-07-12

    // DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, dd MMMM yyyy");
    String formattedDate = date.format(formatter);
    System.out.println(formattedDate); // Friday, 12 July 2024
  }

  // Main driver method
  public static void main3(String[] args) {
    // Getting the current current time
    Date date = new Date();

    System.out.println("Current Time is : " + date);

    // set format in 12 hours
    SimpleDateFormat formatTime = new SimpleDateFormat("hh.mm aa");
    // hh = hours in 12hr format
    // mm = minutes
    // aa = am/pm

    String time = formatTime.format(
        date); // changing the format of 'date'

    // display time as per format
    System.out.println(
        "Current Time in AM/PM Format is : " + time);
  }

  // Method 1
  // To convert String to Date
  public static LocalDate getDateFromString(String string,
      DateTimeFormatter format) {
    // Converting the string to date
    // in the specified format
    LocalDate date = LocalDate.parse(string, format);

    // Returning the converted date
    return date;
  }

  // Method 2
  // Main driver method
  public static void main2(String[] args) {
    // Getting the custom string input
    String string = "28 October, 2018";

    // Getting the format by creating an object of
    // DateTImeFormatter class
    DateTimeFormatter format = DateTimeFormatter.ofPattern("d MMMM, yyyy");

    // Try block to check for exceptions
    try {

      // Getting the Date from String
      LocalDate date = getDateFromString(string, format);

      // Printing the converted date
      System.out.println(date);
    }

    // Block 1
    // Catch block to handle exceptions occurring
    // if the String pattern is invalid
    catch (IllegalArgumentException e) {

      // Display the exception
      System.out.println("Exception: " + e);
    }

    // Block 2
    // If the String was unable to be parsed
    catch (DateTimeParseException e) {

      // Display the exception
      System.out.println("Exception: " + e);
    }
  }

  // Method
  // To convert String to Date
  public static Instant getDateFromString(String string) {
    // Creating an instant object
    Instant timestamp = null;

    // Parsing the string to Date
    timestamp = Instant.parse(string);

    // Returning the converted timestamp
    return timestamp;
  }

  // Method 2
  // Main driver method
  public static void main1(String[] args) {
    // Getting the string
    String string = "2018-10-28T15:23:01Z";

    // Try block to check for exceptions
    try {

      // Getting the Date from String by
      // creating object of Instant class
      Instant timestamp = getDateFromString(string);

      // Printing the converted date
      System.out.println(timestamp);
    }

    // Catch block to handle exceptions
    catch (DateTimeParseException e) {

      // Throws DateTimeParseException
      // if the string cannot be parsed
      System.out.println("Exception: " + e);
    }
  }
}
